package com.dq.controller.op.opmerchant;

import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.op.opmerchant.in.query.BrandQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.brand.Brand;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.dq.validator.ValidatorUtils;
import com.dq.validator.group.AddGroup;
import com.dq.validator.group.UpdateGroup;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author gewenzhen
 */
@Api(description = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends AllService {
    @ApiOperation("所有品牌列表")
    @PostMapping("/list")
    @RequiresPermissions("brandList:list")
    public Vo list(@RequestBody BrandQuery brandQuery) throws AdminException {
        //只有超级管理员，才能查看所有管理员列表
        brandQuery.format();
        Map<String, Object> query = brandQuery.parseMap();
        if (!shiroService.isSuperAdmin()) {
            query.put("org", shiroService.getDept());
        }
        List<Brand> list = dao.find(dao.queryAdminHql(Brand.class, query), query);
        PageHelper.startPage(brandQuery.getPage(), brandQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<Brand.Vo>) VO.toVOCollection(list));
        pageInfo.setTotal(dao.count(dao.countAdminHql(Brand.class, query), query));
        return new Vo(pageInfo);
    }

    @ApiOperation("品牌opention")
    @PostMapping("/all")
    @RequiresPermissions("brandList:all")
    public Vo all() {
        if (shiroService.isSuperAdmin()) {
            List<Brand> brands = brandRepository.findByDeletedIsFalse();
            return new Vo(VO.toVOCollection(brands, false));
        }
        List<Brand> brands = brandRepository.findByOrgAndDeletedFalse(shiroService.getDept());
        return new Vo(VO.toVOCollection(brands, false));
    }

    @ApiOperation("保存品牌")
    @PostMapping("/save")
    @RequiresPermissions("brandList:save")
    public Vo save(@RequestBody Brand brand) throws AdminException {
        brand.setOrg(shiroService.getDept());
        ValidatorUtils.validateEntity(brand, AddGroup.class);
        brand.setCreateUser(shiroService.getUser());
        brandRepository.save(brand);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("修改品牌")
    @PostMapping("/update/{brandId}")
    @RequiresPermissions("brandList:update")
    public Vo update(@RequestBody Brand updateBrand, @PathVariable @ApiParam(value = "品牌id") Long brandId) throws
            AdminException {
        ValidatorUtils.validateEntity(updateBrand, UpdateGroup.class);

        Brand brand = dao.get(Brand.class, brandId);
        if (brand == null) {
            throw new AdminException("品牌不存在");
        }
        brand.setUpdateUser(shiroService.getUser());
        BeanUtils.copyProperties(updateBrand, brand, BeanNullUtil.getNullPropertyNames(updateBrand));
        brandRepository.save(brand);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除品牌")
    @SysLog("删除品牌")
    @PostMapping("/delete/{brandId}")
    @RequiresPermissions("brandList:del")
    public Vo delete(@PathVariable @ApiParam(value = "品牌id") Long brandId) throws AdminException {
        Brand brand = dao.get(Brand.class, brandId);
        if (null == brand) {
            throw new AdminException("品牌不存在");
        }
        brand.setDeleted(true);
        brandRepository.save(brand);
        return new Vo(HttpStatus.OK);
    }
}
